/**
 * @author:fangxiaohua
*  @邮箱：abc2710712@qq.com
*  @qq:1295168875
*  公共工具
*/

void(function(exports){
	//md5加密模块
	var crypto = require('crypto');
	
	//md5加密
	exports.md5=function(text) {
		  return crypto.createHash('md5').update(text).digest('hex');
	};
	
	//获取当前日期
	exports.nowDay=function(){     
		  var date = new Date();     
		  var year = date.getFullYear();     
		  var month = date.getMonth()+1;    
		  var day = date.getDate();     
		  return year + "-" + month + "-" + day;
	},
	//获取当前时间
	exports.nowTime=function(){
		  var date = new Date();     
		  var year = date.getFullYear();     
		  var month = date.getMonth()+1;    
		  var day = date.getDate();     
		  //var week = date.getDay();
		  var hours = date.getHours();     
		  var minutes = date.getMinutes();    
		  var seconds = date.getSeconds();     
		  //var milliseconds = date.getMilliseconds();  
		  return year + "-" + month + "-" + day+ " " + hours + ":" + minutes + ":" + seconds;
	}
	//DES加解密
	exports.DES={
			//加密
			encryptDES:function (data, key) {
				var key="12345678";
				return  this.cipheriv(crypto.createCipheriv('des', key,key), 'utf8', data).toString('base64');
			},
			//解密
			decryptDES:function (data, key) {
				var key="12345678";
				return this.cipheriv(crypto.createDecipheriv('des', key, key), 'base64', data) .toString('utf8');
			},
			cipheriv:function (en, code, data){
				var buf1 = en.update(data, code), 
						buf2 = en.final();     
				var r = new Buffer(buf1.length + buf2.length);     
				buf1.copy(r);
				buf2.copy(r, buf1.length);      
				return r;
			}
		};
	
	
})( (function(){
    if(typeof exports === 'undefined') {
        window.exports = {};
        return window.exports;
    } else {
        return exports;
    }
})());